1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.math;
16
17 import com.google.common.annotations.GwtCompatible;
18
19 import java.math.BigInteger;
20
21 import javax.annotation.Nullable;
22
23
24
25
26
27
28 @GwtCompatible
29 final class MathPreconditions {
30 static int checkPositive(@Nullable String role, int x) {
31 if (x <= 0) {
32 throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
33 }
34 return x;
35 }
36
37 static long checkPositive(@Nullable String role, long x) {
38 if (x <= 0) {
39 throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
40 }
41 return x;
42 }
43
44 static BigInteger checkPositive(@Nullable String role, BigInteger x) {
45 if (x.signum() <= 0) {
46 throw new IllegalArgumentException(role + " (" + x + ") must be > 0");
47 }
48 return x;
49 }
50
51 static int checkNonNegative(@Nullable String role, int x) {
52 if (x < 0) {
53 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
54 }
55 return x;
56 }
57
58 static long checkNonNegative(@Nullable String role, long x) {
59 if (x < 0) {
60 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
61 }
62 return x;
63 }
64
65 static BigInteger checkNonNegative(@Nullable String role, BigInteger x) {
66 if (x.signum() < 0) {
67 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
68 }
69 return x;
70 }
71
72 static double checkNonNegative(@Nullable String role, double x) {
73 if (!(x >= 0)) {
74 throw new IllegalArgumentException(role + " (" + x + ") must be >= 0");
75 }
76 return x;
77 }
78
79 static void checkRoundingUnnecessary(boolean condition) {
80 if (!condition) {
81 throw new ArithmeticException("mode was UNNECESSARY, but rounding was necessary");
82 }
83 }
84
85 static void checkInRange(boolean condition) {
86 if (!condition) {
87 throw new ArithmeticException("not in range");
88 }
89 }
90
91 static void checkNoOverflow(boolean condition) {
92 if (!condition) {
93 throw new ArithmeticException("overflow");
94 }
95 }
96
97 private MathPreconditions() {}
98 }